var NEDB = require("./nedbService");
var co = require("co");
var appApi = require('./../api/app-api');
const storeListMemory = {};

const OrderListNedb = (function(NEDB) {
  function save(data) {
    var ctx = new NEDB.MStoreDataContext();
    var orderList = new NEDB.OrderList();
    orderList.storeId = data.storeId;
    orderList.orderId = data.orderId;
    orderList.id = data.orderId;
    orderList.openid = data.openid;
    orderList.createTime = data.createTime;
    return ctx.Create(orderList);
  }

  function query(param) {
    var ctx = new NEDB.MStoreDataContext();
    let fn;
    if (!param.storeId) {
      fn = (x) => x.openid ==
        param.openid
    } else {
      fn = (x) => x.openid ==
        param.openid && x.storeId ==
        param.storeId
    }
    return ctx.OrderList.Where(fn).ToList()
  }



  return {
    save, query
  }
})(NEDB)



exports.OrderListNedb = OrderListNedb

const StoreNedb = (function(NEDB) {
  function getSerialNo(storeId) {
    return co(function*() {
      if (!storeId) {
        return "storeId 为空"
      }
      try {
        let CurrentSerialNo;
        if (!storeListMemory[storeId]) {
          storeListMemory[storeId] = {}
          storeListMemory[storeId].CurrentSerialNo = 1;
        }
        CurrentSerialNo = storeListMemory[storeId].CurrentSerialNo;
        storeListMemory[storeId].CurrentSerialNo++;
        // var ctx = new NEDB.MStoreDataContext();
        // var store = yield ctx.Store.First((x) => x.id == storeId);
        // if (!store) {
        //   store = new NEDB.Store();
        //   store.id = storeId;
        //   store.storeId = storeId;
        //   store.CurrentSerialNo = CurrentSerialNo;
        //   store.CurrentSerialNo++;
        //   yield ctx.Create(store);
        // } else {
        //   CurrentSerialNo = store.CurrentSerialNo;
        //   store.CurrentSerialNo++;
        //   yield ctx.Update(store);
        // }
        return CurrentSerialNo;
      } catch (e) {
        console.log(e);
        throw (new Error(e))
      }

    });
  }

  function resetStoreSerialNo(storeId) {
    return co(function*() {
      // var ctx = new NEDB.MStoreDataContext();
      // if (storeId) {} else {
      //   var storeList = yield ctx.Store.Where(v => true).ToList();
      //   storeList.forEach(v => {
      //     storeListMemory[v.storeId] =1;
      //     v.CurrentSerialNo = 1;
      //   })
      //   var res = yield ctx.UpdateRange(storeList);
      //   console.log(res);
      // }
      for (var variable in storeListMemory) {
        if (storeListMemory.hasOwnProperty(variable)) {
          storeListMemory[variable].CurrentSerialNo = 1;
        }
      }
    })
  }

  function getStoreInfo(storeId) {
    return co(function*() {
      const ctx = new NEDB.MStoreDataContext();
      let store;
      if (storeId) {
        store = yield ctx.Store.First((x) => x.id == storeId);
        if (!store) {
          let resultWechatInfo = yield appApi(storeId).get(
            '/mstoresetting/info?isCode=true&weixin-order=true');
          store = new NEDB.Store();
          store.id = resultWechatInfo.data.storeId
          store.storeId = resultWechatInfo.data.storeId;
          store.appid = resultWechatInfo.data.wechatAppid;
          store.appsecret = resultWechatInfo.data.wechatAppsecret;
          store.partnerKey = resultWechatInfo.data.wechatApiKey;
          store.mchId = resultWechatInfo.data.wechatMchId;
          store.CurrentSerialNo = (store && store.CurrentSerialNo) || 1;
          store = yield ctx.Create(store);
        } else if (!store.appid) {
          let resultWechatInfo = yield appApi(storeId).get(
            '/mstoresetting/info?isCode=true&weixin-order=true');
          store = new NEDB.Store();
          store.id = resultWechatInfo.data.storeId
          store.storeId = resultWechatInfo.data.storeId;
          store.appid = resultWechatInfo.data.wechatAppid;
          store.appsecret = resultWechatInfo.data.wechatAppsecret;
          store.partnerKey = resultWechatInfo.data.wechatApiKey;
          store.mchId = resultWechatInfo.data.wechatMchId;
          store = yield ctx.Update(store);
        }
        return store;
      } else {
        throw new Error("storeId为空");
      }
    })
  }


  function getWechatInfo(storeId) {
    return co(function*() {
      const ctx = new NEDB.MStoreDataContext();
      let store;
      if (storeId) {
        let flag = false;
        store = yield ctx.Store.First((x) => x.id == storeId);
        if (!store) {
          let resultWechatInfo = yield appApi(storeId).get(
            '/mstoresetting/info?isCode=true&weixin-order=true');
          store = new NEDB.Store();
          store.id = resultWechatInfo.data.storeId
          store.storeId = resultWechatInfo.data.storeId;
          store.appid = resultWechatInfo.data.wechatAppid;
          store.appsecret = resultWechatInfo.data.wechatAppsecret;
          store.partnerKey = resultWechatInfo.data.wechatApiKey;
          store.mchId = resultWechatInfo.data.wechatMchId;
          store.CurrentSerialNo = (store && store.CurrentSerialNo) || 1;
          store = yield ctx.Create(store);
        } else {
          let resultWechatInfo = yield appApi(storeId).get(
            '/mstoresetting/info?isCode=true&weixin-order=true');
          if (store.appid != resultWechatInfo.data.wechatAppid) {
            flag = true;
          } else if (store.appsecret != resultWechatInfo.data.wechatAppsecret) {
            flag = true;
          } else if (store.partnerKey != resultWechatInfo.data.wechatApiKey) {
            flag = true;
          } else if (store.mchId != resultWechatInfo.data.wechatMchId) {
            flag = true;
          }
          if (flag) {
            store.appid = resultWechatInfo.data.wechatAppid;
            store.appsecret = resultWechatInfo.data.wechatAppsecret;
            store.partnerKey = resultWechatInfo.data.wechatApiKey;
            store.mchId = resultWechatInfo.data.wechatMchId;
            store = yield ctx.Update(store);
          }
        }

        return store;
      } else {
        throw new Error("storeId为空");
      }
    })
  }

  return {
    getSerialNo, resetStoreSerialNo, getStoreInfo, getWechatInfo
  }
})(NEDB)



exports.StoreNedb = StoreNedb
